package com.richie696;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.richie696.mapper.ResourceDAO;
import com.richie696.dao.UserDAO;
import com.richie696.dao.User2Mapper;
import com.richie696.domain.ResourceInfo;
import com.richie696.domain.User2Info;
import com.richie696.domain.UserInfo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;
import tk.mybatis.mapper.entity.Example;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Objects;

public class MybatisTest {

    private SqlSession sqlSession1;
    private UserDAO userDAO;
    private UserDAO userDAO2;

    @Before
    public void before() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        sqlSession1 = sqlSessionFactory.openSession();
        SqlSession sqlSession2 = sqlSessionFactory.openSession();
        userDAO = sqlSession1.getMapper(UserDAO.class);
        userDAO2 = sqlSession2.getMapper(UserDAO.class);
    }

    @Test
    public void findById() {
        UserInfo user = userDAO.findUserById(1L);
        System.out.println(user);
        // 不执行commit/close的这些函数是不会将cache写入Redis缓存。
        sqlSession1.commit();

        UserInfo user2 = userDAO2.findUserById(1L);
        System.out.println(user2);

        System.out.println(user == user2);
    }

    @Test
    public void findAll() {
        List<UserInfo> users = userDAO.findAll();
        users.forEach(o -> System.out.println(o.getOrders()));
        sqlSession1.commit();
    }

    @Test
    public void pageHelperTest() {
        int pageNum = 1;
        int pageSize = 5;
        for (int i = 0; i < 5; i++) {
            PageHelper.startPage(pageNum, pageSize);
            List<UserInfo> users = userDAO.findAll();
            PageInfo<UserInfo> pageInfo = null;
            if (Objects.nonNull(users)) {
                pageInfo = new PageInfo<>(users);
            }
            System.out.printf("================ 第%d页 开始 ================%n【内容】：%n", pageNum);
            users.forEach(o -> System.out.printf("UID: %s, Name: %s%n", o.getId(), o.getName()));
            System.out.println("\n【分页信息】：");
            if (pageInfo != null) {
                System.out.printf("总条数：%d%n", pageInfo.getTotal());
                System.out.printf("总页数：%d%n", pageInfo.getPages());
                System.out.printf("上一页：%d%n", pageInfo.getPrePage());
                System.out.printf("当前页：%d%n", pageInfo.getPageNum());
                System.out.printf("下一页：%d%n", pageInfo.getNextPage());
                System.out.printf("最后一页：%d%n", pageInfo.getEndRow());
                System.out.printf("每页显示的条数：%d%n", pageInfo.getPageSize());
            }
            System.out.printf("================ 第%d页 结束 ================%n", pageNum++);
            sqlSession1.commit();
        }

    }

    @Test
    public void mapperHelperTest() {
        /*
             Demo演示未集成Spring，无法使用高于3.1.3版本的”通用mapper“插件，
             Demo演示仅供参考。后续版本完全依赖Spring，否则无法使用，用法可参照
             https://github.com/abel533/Mapper/wiki/2.1-simple
         */
        User2Mapper userMapper = sqlSession1.getMapper(User2Mapper.class);
        User2Info queryParam = new User2Info();
        List<User2Info> users = userMapper.select(queryParam);
        users.forEach(System.out::println);
    }

    @Test
    public void mapperExampleTest() {
        /*
             Demo演示未集成Spring，无法使用高于3.1.3版本的”通用mapper“插件，
             Demo演示仅供参考。后续版本完全依赖Spring，否则无法使用，用法可参照
             https://github.com/abel533/Mapper/wiki/2.1-simple
         */
        User2Mapper userMapper = sqlSession1.getMapper(User2Mapper.class);

        Example example = new Example(User2Info.class);
        Example.Criteria criteria = example.createCriteria()
                .andLike("name", "test%");
        List<User2Info> users = userMapper.selectByExample(example);
        users.forEach(System.out::println);
    }

    @Test
    public void resourceAllTest() {
        ResourceDAO resourceDAO = sqlSession1.getMapper(ResourceDAO.class);
        List<ResourceInfo> resourceList = resourceDAO.findResources(null);
        resourceList.forEach(System.out::println);
        sqlSession1.commit();
    }

    @Test
    public void resourceOneTest() {
        ResourceDAO resourceDAO = sqlSession1.getMapper(ResourceDAO.class);
        ResourceInfo resourceInfo = resourceDAO.findOne(1L);
        System.out.println(resourceInfo.getResourceName());
        System.out.println(resourceInfo.getElements());
        sqlSession1.commit();
    }
}
